package com.yndsoft.jc.supplier.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.yndsoft.jc.R;

public class SupplierDialog {

    private Context context;
    private Dialog dialog;
    private OnPositiveListener positiveListener;
    private OnNegativeListener negativeListener;

    public SupplierDialog(Context context) {
        this.context = context;
    }

    public void show(boolean hasCancel, String title, String content) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
        }
        try {
            dialog = new Dialog(context, R.style.progress_dialog);
            dialog.setContentView(R.layout.supplier_dialog);

            TextView hintTitle = (TextView) dialog.findViewById(R.id.hint_title);
            TextView hintContent = (TextView) dialog.findViewById(R.id.hint_content);
            Button hintCancel = (Button) dialog.findViewById(R.id.hint_cancel);
            Button hintConfirm = (Button) dialog.findViewById(R.id.hint_confirm);

            if (hasCancel) {
                hintCancel.setVisibility(View.VISIBLE);
                hintConfirm.setBackgroundResource(R.drawable.shape_supplier_dialog_right);
            } else {
                hintCancel.setVisibility(View.GONE);
                hintConfirm.setBackgroundResource(R.drawable.shape_supplier_dialog_center);
            }

            hintTitle.setText(title);
            hintContent.setText(content);

            dialog.setCancelable(true);
            dialog.show();

            hintCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (negativeListener != null) {
                        negativeListener.onNegative();
                    }
                    dismissProgressDialog();
                }
            });
            hintConfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (positiveListener != null) {
                        positiveListener.onPositive();
                    }
                    dismissProgressDialog();
                }
            });
        } catch (Exception exception) {
        }
    }

    private void dismissProgressDialog() {
        if (null != dialog && dialog.isShowing() == true) {
            dialog.dismiss();
            dialog = null;
        }
    }

    public void setPositiveListener(OnPositiveListener positiveListener) {
        this.positiveListener = positiveListener;
    }

    public void setNegativeListener(OnNegativeListener negativeListener) {
        this.negativeListener = negativeListener;
    }
}
